package xin.yogee.ms.module.oa.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import xin.yogee.ms.common.BaseController;
import xin.yogee.ms.module.oa.entity.Category;
import xin.yogee.ms.module.oa.service.ICategoryService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 分类 前端控制器
 * </p>
 *
 * @author jackieQ
 * @since 2020-02-27
 */
@RestController
@RequestMapping("/oa/category")
public class CategoryController extends BaseController {
    @Autowired
    private ICategoryService categoryService;

    @RequestMapping(value = "/list")
    public String list(Category category, HttpServletRequest request){
        String current = request.getParameter("currentPage") == null ? "1" : request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize");
        IPage<Category> categoryList = categoryService.page(new Page<Category>(Long.valueOf(current),Long.valueOf(pageSize)));
        return successJson("success",categoryList,"获取成功");
    }


    @RequestMapping(value = "/all")
    public String list(@RequestBody JSONObject json){
        List<Category> categoryList = categoryService.list();
        return successJson("success",categoryList,"获取成功");
    }

    @RequestMapping(value = "/store")
    @ResponseBody
    public String store(@RequestBody JSONObject json, Category category){
        String id = json.getString("id");
        String name = json.getString("name");
        category.setId(id);
        category.setName(name);
        categoryService.saveOrUpdate(category);
        return successJson("success", new HashMap(0), "创建成功");
    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody JSONObject json){
        String id = json.getString("cateId");
        categoryService.removeById(id);
        return successJson("success","","删除成功");
    }
}
